<?php
require_once 'util/class.dbservice.php';

function sortRecord($a, $b) {
	return $a['nid']-$b['nid'];
}

class PrivateBoxDao {
	
	 public function getRecentRecord($userId) {
	 	try {
	 		$collection = $this->getCollection($userId);
	 		if(!$collection)
	 			return;
	
	 		$data = $collection->group(array('ouid'=>true),
	 									array('nid'=>0),
	 									'function(doc, initObj){if(doc.nid>initObj.nid) initObj.nid=doc.nid;}',
	 									array('uid'=>$userId));
	 		$data=$data['retval'];
	 		uasort($data, 'sortRecord');
	 		return $data;
	 	} catch(Exception $e) {
	 		syslog(LOG_ERR, "PrivateBoxDao.getRecentRecord error, reason=".$e);
	 	}
	 }

	 public function getAllRecord($userId, $otherUserId) {
	 	try {
	 		$collection = $this->getCollection($userId);
	 		if(!$collection)
	 			return;
	
	 		$cursor = $collection->find(array('uid'=>$userId, 'ouid'=>$otherUserId),
	 									array('_id'=>0))->sort(array('nid'=>1));
	 		$result=array();
	 		foreach ($cursor as $doc) {
	 			$result[]=$doc;
	 		}
	 		return $result;
	 		
	 	} catch(Exception $e) {
	 		syslog(LOG_ERR, "PrivateBoxDao.getAllRecord error, reason=".$e);
	 	}
	 }
	 
	 public function addItem($id, $userId, $noteId, $otherUserId, $type, $timestamp) {
	 	try {
	 		$collection = $this->getCollection($userId);
	 		if(!$collection)
	 			return;
	 		
	 		$collection->insert(array('_id'=>$id, 'uid'=>$userId, 'nid'=>$noteId,
	 							'ouid'=>$otherUserId, 'tp'=>$type, 'ts'=>$timestamp));
	 	} catch(Exception $e) {
	 		syslog(LOG_ERR, "PrivateBoxDao.addItem error, reason=".$e);
	 	}
	 }
	 
	 public function getList($userId, $type, $pageSize, $sinceNoteId) {
	 	try {
	 		$collection = $this->getCollection($userId);
	 		if(!$collection)
	 			return;
	 		
	 		if($type==Constants::$PRIVATEBOX_TYPE_ALL)
	 			if($sinceNoteId==0)
	 				$cursor=$collection->find(array('uid'=>$userId, 'nid'=>array('$gt'=>$sinceNoteId)),
	 						array('_id'=>0))->sort(array('nid'=>-1))->limit($pageSize);
	 			else
	 				$cursor=$collection->find(array('uid'=>$userId, 'nid'=>array('$lt'=>$sinceNoteId)),
	 						array('_id'=>0))->sort(array('nid'=>-1))->limit($pageSize);
	 		else
		 		if($sinceNoteId==0)
		 			$cursor=$collection->find(array('uid'=>$userId, 'tp'=>$type, 'nid'=>array('$gt'=>$sinceNoteId)),
		 					array('_id'=>0))->sort(array('nid'=>-1))->limit($pageSize);
		 		else
			 		$cursor=$collection->find(array('uid'=>$userId, 'tp'=>$type, 'nid'=>array('$lt'=>$sinceNoteId)),
			 				array('_id'=>0))->sort(array('nid'=>-1))->limit($pageSize);
	 		
	 		$result=array();
	 		foreach ($cursor as $doc) {
	 			$result[]=$doc;
	 		}
	 		return $result;
	 		
	 	} catch(Exception $e) {
	 		syslog(LOG_ERR, "PrivateBoxDao.getInboxList error, reason=".$e);
	 		return null;
	 	}
	 }
	 
	 public function getListByTimestamp($userId, $type, $pageSize, $sinceTimestamp) {
	 	try {
	 		$collection = $this->getCollection($userId);
	 		if(!$collection)
	 			return;
	 		
	 		if($type==Constants::$PRIVATEBOX_TYPE_ALL) {
		 		if($sinceTimestamp==0)
		 			$cursor=$collection->find(array('uid'=>$userId, 'tp'=>$type, 'ts'=>array('$gt'=>$sinceTimestamp)),
		 					array('_id'=>0))->sort(array('ts'=>-1))->limit($pageSize);
		 		else
		 			$cursor=$collection->find(array('uid'=>$userId, 'tp'=>$type, 'ts'=>array('$lt'=>$sinceTimestamp)),
		 					array('_id'=>0))->sort(array('ts'=>-1))->limit($pageSize);
	 		}
	 		else {
		 		if($sinceTimestamp==0)
		 			$cursor=$collection->find(array('uid'=>$userId, 'tp'=>$type, 'ts'=>array('$gt'=>$sinceTimestamp)),
		 					array('_id'=>0))->sort(array('ts'=>-1))->limit($pageSize);
		 		else
			 		$cursor=$collection->find(array('uid'=>$userId, 'tp'=>$type, 'ts'=>array('$lt'=>$sinceTimestamp)),
			 				array('_id'=>0))->sort(array('ts'=>-1))->limit($pageSize);
	 		}
	
	 		$result=array();
	 		foreach ($cursor as $doc) {
	 			$result[]=$doc;
	 		}
	 		return $result;
	 
	 	} catch(Exception $e) {
	 		syslog(LOG_ERR, "PrivateBoxDao.getListByTimestamp error, reason=".$e);
	 		return null;
	 	}
	 }
	 
	 private function getCollection($userId) {
	 	$db=DBService::getDbById('user', $userId);
	 	$collectionName = DBService::getCollectionNameById('privatebox', $userId);
	 	return $db->$collectionName;
	 }
}